﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using Cards.Util;
using Cards.GUI;

namespace Cards.Objects
{
    public class CVNPockerComputer : CVNPockerPlayer
    {
        public CVNPockerComputer(String _name, INS_Player _insPlayer) : base(_name, _insPlayer)
        {            
        }

        public Boolean GenerateMove(ArrayList _array)
        {
            //test
            RemainCards();
            //

            if (_array.Count == 1)
            {
                if (FindSingleCardToPlay(_array))
                {
                    return true;
                }
            }
            else if (_array.Count == 2)
            {
                if (FindCoupleCardsToPlay(_array))
                {
                    return true;
                }
            }
            else
            {
                if (CheckRule_VNPocker.CheckSortedArray(_array))
                {
                    if (FindSortedArrayToPlay(_array))
                    {
                        return true;
                    }
                }
                else
                {
                    if (CheckRule_VNPocker.Check3CardsOfKind(_array))
                    {
                        if (Find3CardsOfKindToPlay(_array))
                        {
                            return true;
                        }
                    }
                    else if (CheckRule_VNPocker.Check4CardsOfKind(_array))
                    {
                        if (CheckGreater4CardsOfKind(_array))
                        {
                            return true;
                        }
                    }
                    else if (CheckRule_VNPocker.CheckPine(_array))
                    {
                        if (GreaterPine(_array))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public Boolean FindSingleCardToPlay(ArrayList _array)
        {
            if (((INS_Card)_array[0]).CARD.CARDVALUE == Constants.GT_HEO)
            {
                if (m_pine != null && m_pine.Count > 0)
                {
                    for (int i = 0; i < m_pine.Count; i++)
                    {
                        ((SCouple)m_pine[i]).a.SelectCard();
                        ((SCouple)m_pine[i]).b.SelectCard();
                    }
                    return true;
                }
                else if (m_4cards != null)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        if (m_4cards[i] != null && m_4cards[i].Count > 0)
                        {
                            for (int j = 0; j < m_4cards[i].Count; j++)
                            {
                                ((INS_Card)m_4cards[i][j]).SelectCard();
                            }
                            return true;
                        }
                    }
                }
            }
            if (m_singleCards != null)
            {
                for (int i = 0; i < m_singleCards.Count; i++)
                {
                    if (((INS_Card)m_singleCards[i]).CARD.CARDTYPE
                        > ((INS_Card)_array[0]).CARD.CARDTYPE)
                    {
                        ((INS_Card)m_singleCards[i]).SelectCard();
                        return true;
                    }
                }
            }
            else if (RemainCards() == 2 && m_coupleCards != null)
            {
                if (((SCouple)m_coupleCards[0]).a.CARD.CARDTYPE > ((INS_Card)_array[0]).CARD.CARDTYPE)
                {
                    ((SCouple)m_coupleCards[0]).a.SelectCard();
                }
                else if (((SCouple)m_coupleCards[0]).b.CARD.CARDTYPE > ((INS_Card)_array[0]).CARD.CARDTYPE)
                {
                    ((SCouple)m_coupleCards[0]).b.SelectCard();
                }
            }
            else if (INS_VNPockerStage.sRemainPlayers == 2)
            {
                if (m_coupleCards != null)
                {
                    for (int i = 0; i < m_coupleCards.Count; i++)
                    {
                        if (((SCouple)m_coupleCards[i]).a.CARD.CARDTYPE >
                            ((INS_Card)_array[0]).CARD.CARDTYPE)
                        {
                            ((SCouple)m_coupleCards[i]).a.SelectCard();
                            return true;
                        }
                        else if (((SCouple)m_coupleCards[i]).b.CARD.CARDTYPE >
                            ((INS_Card)_array[0]).CARD.CARDTYPE)
                        {
                            ((SCouple)m_coupleCards[i]).b.SelectCard();
                            return true;
                        }
                    }
                }
                else if (m_sortedArray != null)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        if (m_sortedArray[i] != null && m_sortedArray[i].Count > 0)
                        {
                            for (int j = 0; j < m_sortedArray[i].Count; j++)
                            {
                                if (((INS_Card)m_sortedArray[i][j]).CARD.CARDTYPE >
                                        ((INS_Card)_array[0]).CARD.CARDTYPE)
                                {
                                    ((INS_Card)m_sortedArray[i][j]).SelectCard();
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            if (m_coupleCards != null && m_coupleCards.Count > 0 &&
                ((SCouple)m_coupleCards[m_coupleCards.Count - 1]).a.CARD.CARDVALUE ==
                                Constants.GT_HEO)
            {
                if (((SCouple)m_coupleCards[m_coupleCards.Count - 1]).a.CARD.CARDTYPE >
                    ((INS_Card)_array[0]).CARD.CARDTYPE)
                {
                    ((SCouple)m_coupleCards[m_coupleCards.Count - 1]).a.SelectCard();
                    return true;
                }
                else if (((SCouple)m_coupleCards[m_coupleCards.Count - 1]).b.CARD.CARDTYPE >
                    ((INS_Card)_array[0]).CARD.CARDTYPE)
                {
                    ((SCouple)m_coupleCards[m_coupleCards.Count - 1]).b.SelectCard();
                    return true;
                }
            }
            else if (m_3cards != null)
            {
                for (int i = 0; i < 4; i++)
                {
                    if (m_3cards[i] != null)
                    {
                        if (((INS_Card)m_3cards[i][0]).CARD.CARDVALUE ==
                            Constants.GT_HEO)
                        {
                            for (int j = 0; j < m_3cards[i].Count; j++)
                            {
                                if (((INS_Card)m_3cards[i][j]).CARD.CARDTYPE
                                    > ((INS_Card)_array[0]).CARD.CARDTYPE)
                                {
                                    ((INS_Card)m_3cards[i][j]).SelectCard();
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            else if (m_sortedArray != null)
            {
                for (int i = 0; i < 4; i++)
                {
                    if (m_sortedArray[i] != null)
                    {
                        for (int j = 0; j < m_sortedArray[i].Count; j++)
                        {
                            if (((INS_Card)m_sortedArray[i][j]).CARD.CARDTYPE
                                > ((INS_Card)_array[0]).CARD.CARDTYPE)
                            {
                                ((INS_Card)m_sortedArray[i][j]).SelectCard();
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }

        public Boolean FindSortedArrayToPlay(ArrayList _array)
        {
            if (m_sortedArray != null)
            {
                for (int i = 0; i < 4; i++)
                {
                    if (m_sortedArray[i] != null && m_sortedArray[i].Count >= _array.Count)
                    {
                        if (((INS_Card)m_sortedArray[i][m_sortedArray[i].Count - 1]).CARD.CARDTYPE
                            > ((INS_Card)_array[0]).CARD.CARDTYPE)
                        {
                            if (m_sortedArray[i].Count == _array.Count)
                            {
                                for (int j = 0; j < m_sortedArray[i].Count; j++)
                                {
                                    ((INS_Card)m_sortedArray[i][j]).SelectCard();
                                }
                                return true;
                            }
                            else
                            {
                                int k = m_sortedArray[i].Count - 1;
                                while (k >= _array.Count &&
                                    ((INS_Card)m_sortedArray[i][k]).CARD.CARDTYPE >
                                    ((INS_Card)_array[0]).CARD.CARDTYPE)
                                {
                                    k--;
                                }

                                if (((INS_Card)m_sortedArray[i][k]).CARD.CARDTYPE <
                                    ((INS_Card)_array[0]).CARD.CARDTYPE)
                                {
                                    k++;
                                }

                                for (int l = 0; l < _array.Count; l++)
                                {
                                    ((INS_Card)m_sortedArray[i][k--]).SelectCard();
                                }
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }

        public Boolean Find3CardsOfKindToPlay(ArrayList _array)
        {
            if (m_3cards != null && m_3cards[0] != null && m_3cards[0].Count > 0)
            {
                for (int i = 0; i < 4; i++)
                {
                    if (m_3cards[i] != null)
                    {
                        if ((((INS_Card)m_3cards[i][0]).CARD.CARDVALUE !=
                              Constants.GT_HEO &&
                              ((INS_Card)m_3cards[i][0]).CARD.CARDVALUE !=
                              Constants.GT_ACH) &&
                            ((INS_Card)m_3cards[i][0]).CARD.CARDVALUE
                            > ((INS_Card)_array[0]).CARD.CARDVALUE)
                        {
                            for (int j = 0; j < 3; j++)
                            {
                                ((INS_Card)m_3cards[i][j]).SelectCard();
                            }
                            return true;
                        }
                        else if (((INS_Card)_array[0]).CARD.CARDVALUE ==
                              Constants.GT_ACH &&
                            ((INS_Card)m_3cards[i][0]).CARD.CARDVALUE
                                > ((INS_Card)_array[0]).CARD.CARDVALUE)
                        {
                            for (int j = 0; j < 3; j++)
                            {
                                ((INS_Card)m_3cards[i][j]).SelectCard();
                            }
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public Boolean FindCoupleCardsToPlay(ArrayList _array)
        {
            if (((INS_Card)_array[0]).CARD.CARDVALUE == Constants.GT_HEO)
            {
                if (m_pine != null && m_pine.Count >= 4)
                {
                    for (int i = 0; i < m_pine.Count; i++)
                    {
                        ((INS_Card)m_pine[i]).SelectCard();
                    }
                    return true;
                }
                else if (m_4cards != null)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        if (m_4cards[i] != null && m_4cards[i].Count > 0)
                        {
                            for (int j = 0; j < m_4cards[i].Count; j++)
                            {
                                ((INS_Card)m_4cards[i][j]).SelectCard();
                            }
                            return true;
                        }
                    }
                }
            }

            if (m_coupleCards != null)
            {
                for (int i = 0; i < m_coupleCards.Count; i++)
                {
                    if (ArrayUtil.Max(((INS_Card)((SCouple)m_coupleCards[i]).a).CARD.CARDTYPE,
                                      ((INS_Card)((SCouple)m_coupleCards[i]).b).CARD.CARDTYPE)
                        > ArrayUtil.Max(((INS_Card)_array[0]).CARD.CARDTYPE,
                                        ((INS_Card)_array[1]).CARD.CARDTYPE))
                    {
                        if (!InPine((((SCouple)m_coupleCards[i]).a).CARD.CARDTYPE) &&
                            !InPine(((SCouple)m_coupleCards[i]).b.CARD.CARDTYPE) &&
                            !In4Cards((((SCouple)m_coupleCards[i]).a).CARD.CARDTYPE) &&
                            !In4Cards(((SCouple)m_coupleCards[i]).b.CARD.CARDTYPE) &&
                            !InSortedArray(((SCouple)m_coupleCards[i]).a.CARD.CARDTYPE) &&
                            !InSortedArray(((SCouple)m_coupleCards[i]).b.CARD.CARDTYPE))
                        {
                            ((INS_Card)((SCouple)m_coupleCards[i]).a).SelectCard();
                            ((INS_Card)((SCouple)m_coupleCards[i]).b).SelectCard();
                            return true;
                        }
                    }
                }
            }
            else if (((CVNPockerStage)(m_insPlayer.INSSTAGE.STAGE)).NoPigMore())// || INS_Stage.sRemainPlayers == 2)
            {
                if (m_pine != null)
                {
                    for (int i = 0; i < m_pine.Count; i++)
                    {
                        if (ArrayUtil.Max(((INS_Card)((SCouple)m_pine[i]).a).CARD.CARDTYPE,
                                      ((INS_Card)((SCouple)m_pine[i]).b).CARD.CARDTYPE)
                             > ArrayUtil.Max(((INS_Card)_array[0]).CARD.CARDTYPE,
                                        ((INS_Card)_array[1]).CARD.CARDTYPE))
                        {
                            ((INS_Card)((SCouple)m_pine[i]).a).SelectCard();
                            ((INS_Card)((SCouple)m_pine[i]).b).SelectCard();
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public Boolean GreaterPine(ArrayList _array)
        {
            if (m_pine != null && m_pine.Count > _array.Count / 2)
            {
                for (int i = 0; i < m_pine.Count; i++)
                {
                    ((SCouple)m_pine[i]).a.SelectCard();
                    ((SCouple)m_pine[i]).b.SelectCard();
                }
                return true;
            }
            else if (m_pine != null && m_pine.Count == _array.Count / 2)
            {
                if (ArrayUtil.Max(((SCouple)m_pine[m_pine.Count - 1]).a.CARD.CARDTYPE,
                                 ((SCouple)m_pine[m_pine.Count - 1]).b.CARD.CARDTYPE)
                    > ArrayUtil.Max(((INS_Card)_array[_array.Count - 1]).CARD.CARDTYPE,
                                    ((INS_Card)_array[_array.Count - 2]).CARD.CARDTYPE))
                {
                    for (int i = 0; i < m_pine.Count; i++)
                    {
                        ((SCouple)m_pine[i]).a.SelectCard();
                        ((SCouple)m_pine[i]).b.SelectCard();
                    }
                    return true;
                }
            }
            else if (m_4cards != null && m_4cards[0] != null)
            {
                if (_array.Count / 2 == 3)
                {
                    for (int i = 0; i < m_4cards[0].Count; i++)
                    {
                        ((INS_Card)m_4cards[0][i]).SelectCard();
                    }
                    return true;
                }
            }
            return false;
        }

        public Boolean CheckGreater4CardsOfKind(ArrayList _array)
        {
            if (m_4cards != null && m_4cards[0] != null)
            {
                for (int i = 0; i < 4; i++)
                {
                    if (m_4cards[i] != null)
                    {
                        if (((INS_Card)m_4cards[i][0]).CARD.CARDVALUE
                             > ((INS_Card)_array[0]).CARD.CARDVALUE)
                        {
                            for (int j = 0; j < m_4cards[0].Count; j++)
                            {
                                ((INS_Card)m_4cards[i][j]).SelectCard();
                            }
                            return true;
                        }
                    }
                }
            }
            else if (m_pine != null && m_pine.Count >= 4)
            {
                for (int i = 0; i < m_pine.Count; i++)
                {
                    ((SCouple)m_pine[i]).a.SelectCard();
                    ((SCouple)m_pine[i]).b.SelectCard();
                }
                return true;
            }
            return false;
        }

        public int CountSortedArray()
        {
            int count = 0;
            if (m_sortedArray != null)
            {
                for (int i = 0; i < 4; i++)
                {
                    if (m_sortedArray[i] != null && m_sortedArray[i].Count >= 3)
                    {
                        count++;
                    }
                }
            }
            return count;
        }

        public int FindInSingleCards(int _value)
        {
            int idx = -1;
            if (m_singleCards != null)
            {
                for (int i = 0; i < m_singleCards.Count; i++)
                {
                    if (((INS_Card)m_singleCards[i]).CARD.CARDTYPE == _value)
                    {
                        ((INS_Card)m_singleCards[i]).SelectCard();
                        idx = i;
                        break;
                    }
                }
            }
            return idx;
        }
        public int FindInCoupleCards(int _value)
        {
            int idx = -1;

            if (m_coupleCards != null)
            {
                for (int i = 0; i < m_coupleCards.Count; i++)
                {
                    if ((((SCouple)m_coupleCards[i]).a.CARD.CARDTYPE == _value)
                        || (((SCouple)m_coupleCards[i]).b.CARD.CARDTYPE == _value))
                    {
                        ((SCouple)m_coupleCards[i]).a.SelectCard();
                        ((SCouple)m_coupleCards[i]).b.SelectCard();
                        idx = i;
                        break;
                    }
                }
            }
            return idx;
        }
        public int FindIn3Cards(int _value)
        {
            int idx = -1;

            if (m_3cards != null)
            {
                for (int i = 0; i < m_3cards.Length; i++)
                {
                    if (m_3cards[i] != null)
                    {
                        for (int j = 0; j < m_3cards[i].Count; j++)
                        {
                            if (((INS_Card)m_3cards[i][j]).CARD.CARDTYPE == _value)
                            {
                                idx = i;
                                break;
                            }
                        }

                        if (idx != -1)
                        {
                            for (int j = 0; j < m_3cards[i].Count; j++)
                            {
                                ((INS_Card)m_3cards[i][j]).SelectCard();
                            }
                            break;
                        }
                    }
                }
            }

            return idx;
        }
        public int FindInSortedArrayCards(int _value)
        {
            int idx = -1;

            if (m_sortedArray != null)
            {
                for (int i = 0; i < m_sortedArray.Length; i++)
                {
                    if (m_sortedArray[i] != null)
                    {
                        for (int j = 0; j < m_sortedArray[i].Count; j++)
                        {
                            if (((INS_Card)m_sortedArray[i][j]).CARD.CARDTYPE == _value)
                            {
                                idx = i;
                                break;
                            }
                        }

                        if (idx != -1)
                        {
                            for (int j = 0; j < m_sortedArray[i].Count; j++)
                            {
                                ((INS_Card)m_sortedArray[i][j]).SelectCard();
                            }
                            break;
                        }
                    }
                }
            }

            return idx;
        }
        public int FindInPineCards(int _value)
        {
            int idx = -1;

            if (m_pine != null)
            {
                for (int i = 0; i < m_pine.Count; i++)
                {
                    if ((((SCouple)m_pine[i]).a.CARD.CARDTYPE == _value)
                        || (((SCouple)m_pine[i]).b.CARD.CARDTYPE == _value))
                    {
                        idx = i;
                        break;
                    }
                }

                if (idx != -1)
                {
                    for (int i = 0; i < m_pine.Count; i++)
                    {
                        ((SCouple)m_pine[i]).a.SelectCard();
                        ((SCouple)m_pine[i]).b.SelectCard();
                    }
                }
            }
            return idx;
        }
        public int FindIn4Cards(int _value)
        {
            int idx = -1;

            if (m_4cards != null)
            {
                for (int i = 0; i < m_4cards.Length; i++)
                {
                    if (m_4cards[i] != null)
                    {
                        for (int j = 0; j < m_4cards[i].Count; j++)
                        {
                            if (((INS_Card)m_4cards[i][j]).CARD.CARDTYPE == _value)
                            {
                                idx = i;
                                break;
                            }
                        }

                        if (idx != -1)
                        {
                            for (int j = 0; j < m_4cards[i].Count; j++)
                            {
                                ((INS_Card)m_4cards[i][j]).SelectCard();
                            }
                            break;
                        }
                    }
                }
            }
            return idx;
        }

        public void FindACardByValueToPlay(int _value)
        {
            int idx = FindInSingleCards(_value);
            if (idx == -1)
            {
                idx = FindInCoupleCards(_value);
                if (idx == -1)
                {
                    idx = FindIn3Cards(_value);
                    if (idx == -1)
                    {
                        idx = FindInSortedArrayCards(_value);
                        if (idx == -1)
                        {
                            idx = FindInPineCards(_value);
                            if (idx == -1)
                            {
                                idx = FindIn4Cards(_value);
                            }
                        }
                    }
                }
            }
        }

        public void Find3aToPlay()
        {
            FindACardByValueToPlay(Constants.BABICH);
        }

        public void FindSmallestCardToPlay()
        {
            int smallest = ArrayUtil.FindMinCard(this.IP);
            FindACardByValueToPlay(smallest);
        }

        public Boolean GenerateNewCycle()
        {
            //test
            RemainCards();

            if (MainFrame.sFirstPlayInStage)
            {
                if (ArrayUtil.FindCardByCardType(this.IP.IC, Constants.BABICH))
                {
                    Find3aToPlay();
                }
                else
                {
                    FindSmallestCardToPlay();
                }
                return true;
            }

            if ((m_coupleCards.Count > 0) && (m_singleCards != null) && (m_singleCards.Count > 1) &&
                (((INS_Card)m_singleCards[0]).CARD.CARDVALUE
                  > ((SCouple)m_coupleCards[0]).a.CARD.CARDVALUE))
            {
                ((SCouple)m_coupleCards[0]).a.SelectCard();
                ((SCouple)m_coupleCards[0]).b.SelectCard();
                return true;
            }
            else if (m_sortedArray[0] != null && m_sortedArray[0].Count > 0
                && m_singleCards != null && m_singleCards.Count > 1 &&
                ((INS_Card)m_singleCards[0]).CARD.CARDVALUE
                  >= ((INS_Card)m_sortedArray[0][m_sortedArray[0].Count - 1]).CARD.CARDVALUE)
            {
                for (int i = 0; i < m_sortedArray[0].Count; i++)
                {
                    ((INS_Card)m_sortedArray[0][i]).SelectCard();
                }
                return true;
            }
            else if (m_3cards != null && m_3cards[0] != null && m_3cards[0].Count > 0
                && m_sortedArray != null && m_sortedArray[0] != null && m_singleCards != null && m_coupleCards != null
                && ((INS_Card)m_3cards[0][0]).CARD.CARDVALUE < Constants.GT_BOI
                && ((INS_Card)m_3cards[0][0]).CARD.CARDVALUE
                     < ((SCouple)m_coupleCards[m_coupleCards.Count - 1]).a.CARD.CARDVALUE
                )
            {
                for (int i = 0; i < m_3cards[0].Count; i++)
                {
                    ((INS_Card)m_3cards[0][i]).SelectCard();
                }
                return true;
            }
            else if (m_3cards != null && m_3cards[0] != null && m_3cards[0].Count > 0
               && m_sortedArray != null && m_singleCards != null && m_coupleCards != null && m_coupleCards.Count > 0
               && ((INS_Card)m_3cards[0][0]).CARD.CARDVALUE < Constants.GT_BOI
               && ((INS_Card)m_3cards[0][0]).CARD.CARDVALUE
                    < ((SCouple)m_coupleCards[m_coupleCards.Count - 1]).a.CARD.CARDVALUE
               )
            {
                for (int i = 0; i < m_3cards[0].Count; i++)
                {
                    ((INS_Card)m_3cards[0][i]).SelectCard();
                }
                return true;
            }
            else if (m_3cards != null && m_3cards[0] != null && m_3cards[0].Count > 0
              && m_sortedArray != null && m_coupleCards != null
              && ((INS_Card)m_3cards[0][0]).CARD.CARDVALUE < Constants.GT_BOI
              && (m_coupleCards.Count > 0) && ((INS_Card)m_3cards[0][0]).CARD.CARDVALUE
                   < ((SCouple)m_coupleCards[m_coupleCards.Count - 1]).a.CARD.CARDVALUE
              )
            {
                for (int i = 0; i < m_3cards[0].Count; i++)
                {
                    ((INS_Card)m_3cards[0][i]).SelectCard();
                }
                return true;
            }
            else if (m_singleCards != null && m_singleCards.Count > 1)
            {
                ((INS_Card)m_singleCards[0]).SelectCard();
                return true;
            }
            else if (m_sortedArray != null && m_coupleCards != null && m_coupleCards.Count > 0
                     && m_sortedArray[0] != null && m_sortedArray[0].Count > 0
                     && CountSortedArray() >= m_coupleCards.Count
                     && ((INS_Card)m_sortedArray[0][0]).CARD.CARDTYPE
                        < ((SCouple)m_coupleCards[0]).a.CARD.CARDTYPE
                     && !INS_Stage.sBeginPlaying)
            {
                for (int i = 0; i < m_sortedArray[0].Count; i++)
                {
                    ((INS_Card)m_sortedArray[0][i]).SelectCard();
                }
                return true;
            }
            else if (m_3cards != null && m_3cards[0] != null && m_3cards[0].Count > 0)
            {
                for (int i = 0; i < m_3cards[0].Count; i++)
                {
                    ((INS_Card)m_3cards[0][i]).SelectCard();
                }
                return true;
            }
            //else if (m_coupleCards != null && m_coupleCards.Count == 1 && m_singleCards != null && m_singleCards.Count ==1
            //    && ((SCouple)m_coupleCards[0]).a.CARD.CARDVALUE 
            //         > ((TH_QuanBai)m_singleCards[0]).CARD.CARDVALUE
            //    && ((SCouple)m_coupleCards[0]).a.CARD.CARDVALUE -
            //        ((TH_QuanBai)m_singleCards[0]).CARD.CARDVALUE >4)
            //{
            //    ((TH_QuanBai)m_singleCards[0]).SelectCard();
            //    return true;
            //}
            // else if (m_sortedArray != null && CountSortedArray() == 1 && m_singleCards != null && m_singleCards.Count == 1
            //&& ((TH_QuanBai)m_singleCards[0]).CARD.CARDVALUE
            //    < ((TH_QuanBai)m_sortedArray[0][0]).CARD.CARDVALUE)
            // {
            //     ((TH_QuanBai)m_singleCards[0]).SelectCard();
            //     return true;
            // }
            else if (m_sortedArray != null && m_sortedArray[0] != null &&
                    m_sortedArray[0].Count > 0)
            {
                for (int i = 0; i < m_sortedArray[0].Count; i++)
                {
                    ((INS_Card)m_sortedArray[0][i]).SelectCard();
                }
                return true;
            }
            else if (m_coupleCards != null && m_coupleCards.Count > 0)
            {
                ((SCouple)m_coupleCards[0]).a.SelectCard();
                ((SCouple)m_coupleCards[0]).b.SelectCard();
                return true;
            }
            else if (m_pine != null && m_pine.Count == 3)
            {
                for (int i = 0; i < m_pine.Count; i++)
                {
                    ((SCouple)m_pine[i]).a.SelectCard();
                    ((SCouple)m_pine[i]).b.SelectCard();
                }
                return true;
            }
            else if (m_4cards != null && m_4cards[0] != null && m_4cards[0].Count > 0)
            {
                for (int i = 0; i < m_4cards[0].Count; i++)
                {
                    ((INS_Card)m_4cards[0][i]).SelectCard();
                }
                return true;
            }
            else if (m_pine != null && m_pine.Count > 3)
            {
                for (int i = 0; i < m_pine.Count; i++)
                {
                    ((SCouple)m_pine[i]).a.SelectCard();
                    ((SCouple)m_pine[i]).b.SelectCard();
                }
                return true;
            }
            else if (m_singleCards != null && m_singleCards.Count == 1)
            {
                ((INS_Card)m_singleCards[0]).SelectCard();
                return true;
            }

            return false;
        }

        public int RemainCards()
        {
            ArrayList arr = new ArrayList();

            int count = 0;
            for (int i = 0; i < 13; i++)
            {
                if (!m_insPlayer[i].CARD.PLAY)
                {
                    count++;
                    arr.Add(m_insPlayer[i].PICPATH);
                }
            }
            return count;
        }
    }
}
